<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="角色名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入角色名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['allocation:role:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['allocation:role:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['allocation:role:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['allocation:role:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="角色名称" align="center" prop="roleName" />
      <el-table-column label="分组备注" align="center" prop="groupRemark" />
      <el-table-column label="用户列表" align="center" prop="groupRemark">
        <template slot-scope="scope">
          <el-button icon="el-icon-s-custom" size="mini" @click="getRoleList(scope.row)"></el-button>
        </template>
      </el-table-column>
      <el-table-column label="是否默认角色" align="center" prop="isDefault">
        <template slot-scope="scope">
          <el-switch  v-model="scope.row.isDefault"
           active-color="#13ce66"
                 inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                @change="handleIsDefaultChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
      <template slot-scope="scope">
          <el-switch  v-model="scope.row.status"
           active-color="#13ce66"
                 inactive-color="#ff4949"
                :active-value="0"
                :inactive-value="2"
                @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" />
      <el-table-column label="报表权限" align="center" prop="statement" >
      <template slot-scope="scope">
          <el-switch  v-model="scope.row.statement"
           active-color="#13ce66"
                 inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                @change="handleStatementChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="通信录权限" align="center" prop="contacts" >
      <template slot-scope="scope">
          <el-switch  v-model="scope.row.contacts"
           active-color="#13ce66"
                 inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                @change="handleContactsChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="查看权限" align="center" prop="type" >
      <template slot-scope="scope">
          <el-switch  v-model="scope.row.type"
           active-color="#13ce66"
                 inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                @change="handleTypeChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['allocation:role:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['allocation:role:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="form.roleName" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="分组备注" prop="groupRemark">
          <el-input v-model="form.groupRemark" placeholder="请输入分组备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

     <!-- 角色用户数量对话框 -->
    <el-dialog :title="title" :visible.sync="roleOpen" width="70%" append-to-body>
      <el-table v-loading="loading" :data="userRoleList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" :index="createIndex" />
      <el-table-column label="头像" align="center" prop="headUrl" >
         <template   slot-scope="scope">            
                    <img :src="scope.row.headUrl"  min-width="70px" height="70px" 
                           @click="previewPic(scope.row.headUrl)" />
                 </template>  
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nikeName" />
      <el-table-column label="姓名" align="center" prop="realName" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['allocation:role:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['allocation:role:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-dialog>

     <!-- 图片预览   放在表格外面，这是个弹出框-->
      <el-dialog :visible.sync="dialogVisible" :modal="false" title="图片预览" width="50%">
        <img :src="previewpic" alt=""  width="100%" />
        <span slot="footer" class="dialog-footer">
          </span>
       </el-dialog>
  <!-- 图片预览 -->
  </div>
</template>

<script>
import { listRole, getRole, delRole, addRole, 
updateRole,changeNoticeStatus,changeIsDefaultStatus,
changeStatementStatus,changeContactsStatus,
changeTypeStatus,getUserRole } from "@/api/allocation/role";

export default {
  name: "Role",
  data() {
    return {
       dialogVisible: false,
        previewpic: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",

    userRoleList:[],

    roleOpen:false,

      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: null,
        groupRemark: null,
        isDefault: null,
        status: null,
        asId: null,
        statement: null,
        contacts: null,
        type: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        asId: [
          { required: true, message: "协会id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    createIndex(index){
   return (this.queryParams.pageNum -1) *this.queryParams.pageSize +index +1;
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      listRole(this.queryParams).then(res => {
        console.info(res);
        this.roleList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    //图片放大
      previewPic(url) {
           this.previewpic = url;
           this.dialogVisible = true;
        },
    getRoleList(row){
      console.info(row.id);
      const id=row.id
      getUserRole(id).then(res=>{
        console.info(res);
        this.userRoleList=res.rows;
        this.roleOpen=true;
        this.title="角色用户列表"
      })
    },
           // 状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.id + '"公告吗？').then(function() {
        return changeNoticeStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "2" : "0";
      });
    },
           // 默认角色状态修改
    handleIsDefaultChange(row) {
      let text = row.isDefault === "1" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.id + '"公告吗？').then(function() {
        return changeIsDefaultStatus(row.id, row.isDefault);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.isDefault = row.isDefault === "0" ? "2" : "0";
      });
    },
           // 报表权限状态修改
    handleStatementChange(row) {
      let text = row.statement === "1" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.id + '"公告吗？').then(function() {
        return changeStatementStatus(row.id, row.statement);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.statement = row.statement === "0" ? "2" : "0";
      });
    },
           // 通讯录权限状态修改
    handleContactsChange(row) {
      let text = row.contacts === "1" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.id + '"公告吗？').then(function() {
        return changeContactsStatus(row.id, row.contacts);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.contacts = row.contacts === "0" ? "2" : "0";
      });
    },
           // 查看文章权限状态修改
    handleTypeChange(row) {
      let text = row.type === "1" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.id + '"公告吗？').then(function() {
        return changeTypeStatus(row.id, row.type);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.type = row.type === "1" ? "2" : "1";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        roleName: null,
        groupRemark: null,
        isDefault: null,
        createTime: null,
        updateTime: null,
        status: 0,
        asId: null,
        statement: null,
        contacts: null,
        type: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加角色";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRole(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改角色";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRole(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRole(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除角色编号为"' + ids + '"的数据项？').then(function() {
        return delRole(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('allocation/role/export', {
        ...this.queryParams
      }, `role_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
